package gui.nodes;

import gui.AbstractViewPane;
import gui.CongeUIConsulter;
import gui.MainController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import utility.Record;
import business.CongeBL;

public class CongeRoot extends AbstractNode {
	
	int anneeEtudeId;
	private AnneeEtudeNode parentNode;
	
	public CongeRoot(AnneeEtudeNode parentNode, int aeId){
		super();
		this.parentNode = parentNode;
		this.anneeEtudeId = aeId;
	}

	public String getNodeName() {
		return "Congés";
	}

	public void loadPrimaryViewPane() {
	}
	public void loadAjouterViewPane() {
		AbstractViewPane newViewPane = new CongeUIConsulter(parentNode,
				AbstractViewPane.ViewMode.AJOUT, -1, anneeEtudeId);
		MainController.getInstance().setViewPane(newViewPane);
	}
	
	public JPopupMenu makePopup() {

		return new Popup();
	}
	
	private class Popup extends JPopupMenu implements ActionListener {
		JMenuItem ajouter = new JMenuItem("Ajouter un congé");
		
		public Popup() {
			ajouter.addActionListener(this);

			add(ajouter);
		}
		
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == ajouter){
				loadAjouterViewPane();
			}
			
		}
		
	}

	@Override
	public void expandHook() {
		this.removeAllChildren();

		CongeBL congeBL = new CongeBL();
		
		for (Record recordConge: congeBL.getListeConges()) {
			CongeNode congNode = new CongeNode(this, recordConge, anneeEtudeId);
			add(congNode);
		}
	}

}
